package com.yl.plugin;

import com.yl.plugin.model.LombokProperty;
import com.yl.plugin.util.Util;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;

import java.util.List;

/**
 * mybatis-generator实体类上lombok注释插件
 *
 * <p>在contex标签内添加
 * <pre>
 *    &lt;plugin type="com.yl.ext.LomBokPlugin" &gt;
 *       &lt;property name="hasLombok" value="true"/&gt;
 *       &lt;property name="user.name" value="liuxubo"/&gt;
 *       &lt;property name="dateTimeFormat" value="yyyy/MM/dd HH:mm"/&gt;
 *    &lt;/plugin&gt;
 *
 *      必须配置 suppressAllComments 为 true 否则 会重复生成注释
 *      &lt;commentGenerator type="com.yl.ext.ZhCnCommentGenerator"&gt;
 *         &lt;property name="suppressAllComments" value="true" /&gt;
 *       &lt;/commentGenerator&gt;
 *  </pre>
 *
 * @author buyl
 * @date 2019年5月30日 上午22:04:55
 */
public class LombokPlugin extends PluginAdapter {
    private LombokProperty lombokProperty;


    @Override
    public boolean validate(List<String> list) {
        this.lombokProperty = Util.setLombokProperty(properties, context);
        return true;
    }

    /**
     * 模型类
     *
     * @param topLevelClass
     * @param introspectedTable
     */
    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        if (lombokProperty.isEnableLombok()) {
            // 添加domain的import
            topLevelClass.addImportedType("lombok.Data");

            // 添加domain的注解
            topLevelClass.addAnnotation("@Data");
        }

        Util.setClassComment(introspectedTable.getRemarks(), introspectedTable.getContext()).forEach(s -> topLevelClass.addJavaDocLine(s));
        return true;
    }

    /**
     * 实体类属性生成
     */
    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                       IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        Util.setFieldComment(introspectedColumn.getRemarks()).forEach(s -> field.addJavaDocLine(s));
        return true;
    }

    /**
     * 生成 Mapper接口
     *
     * @param interfaze
     * @param introspectedTable
     * @return:boolean
     */
    @Override
    public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {
        Util.setClassComment(introspectedTable.getRemarks(), introspectedTable.getContext()).forEach(s -> interfaze.addJavaDocLine(s));

        if (lombokProperty.isAnnotatedMapper()) {
            // 添加Mapper的注解
            interfaze.addAnnotation("@Mapper");
            // 添加Mapper的import
            interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper"));
        }

        return true;
    }


    /**
     * 生成setter方法
     *
     * @param method
     * @param topLevelClass
     * @param introspectedColumn
     * @param introspectedTable
     * @param modelClassType
     * @return:boolean
     */
    @Override
    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        // 不生成getter
        return !lombokProperty.isEnableLombok();
    }

    /**
     * 生成getter方法
     *
     * @param method
     * @param topLevelClass
     * @param introspectedColumn
     * @param introspectedTable
     * @return:boolean
     */
    @Override
    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        // 不生成setter
        return !lombokProperty.isEnableLombok();
    }

}
